package tictactoe.game;


public abstract class AbstractController implements ControllerInterface
{
        protected int player = 0;
        protected final int X, Y;
        protected int[][] grid;
        boolean all;
        
        public AbstractController(Model model) {
                this.grid = model.getGrid();
                this.X = model.getX();
                this.Y = model.getY();
        }
        
        public int getValueOfGrid(int i,int j)
        {
        	return grid[i][j];
        }
       
        public void show()
    {
        System.out.print("  ");
        for(int i=1;i<=Y;i++) System.out.printf("%d ", i);
        System.out.println();
               
        for(int i=0;i<=2;i++)
        {
                System.out.printf("%d ",i+1);
                for(int j=0;j<=2;j++)
                        if (grid[i][j] == 1) System.out.print("X ");
                        else
                        if (grid[i][j] == 2) System.out.print("O ");
                        else System.out.print("_ ");
            System.out.print("\n");
        }
    }
       
        public boolean checkWon(int[][] board)
        {
                boolean fl;//win=false;
                int i,j;
               
                all=true;
                for(i=0;i<X;i++)
                {
                        for(j=0;j<Y;j++)
                                if (board[i][j] <= 0) { all=false;break;}
                        if (all == false) break;
                }
               
                //check rows
                for(i=0;i<X;i++)
                {
                        fl=true;
                        for(j=1;j<Y;j++)
                                if (board[i][j] != board[i][j-1]) { fl=false;break;}
                        if (fl) { return true;}
                }
                //check columns
                for(j=0;j<Y;j++)
                {
                        fl=true;
                        for(i=1;i<X;i++)
                                if (board[i][j] != board[i-1][j]) { fl=false;break;}
                        if (fl) { return true;}
                }
                //check diagonals
                fl=true;
                for(i=1;i<X;i++)
                        if (board[i][i] != board[i-1][i-1]) { fl=false;break;}
                if (fl) return true;
               
                fl=true;
                for(i=1;i<X;i++)
                        if (board[i][Y-i-1] != board[i-1][Y-i]) { fl=false;break;}
                if (fl) return true;
                
                return false;
        }
       
        @Override
        public int getTurn() {
                return player + 1;
        }
}